package com.example.community.service;

import com.example.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

/**
 * @Author: huang
 * @Date: 2021/9/26 16:30
 * @Description:
 */
@Service
public class LikeService {

    @Autowired
    private RedisTemplate redisTemplate;

    //点赞.

    /**
     *
     * @param userId    当前用户
     * @param entityType    被点赞的类型
     * @param entityId  被点赞的帖子或评论的id
     * @param entityUserId  被点赞的帖子或评论的用户id
     */
    public void like(int userId, int entityType, int entityId, int entityUserId) {
/*        //生成实体对应的key
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        //判断userId是否在set集合里
        boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId);
        if (isMember) {
            //如果存在，再次点赞就是取消点赞，从集合里删除
            redisTemplate.opsForSet().remove(entityLikeKey, userId);
        }else {
            //该用户没点过赞，添加到集合
            redisTemplate.opsForSet().add(entityLikeKey, userId);
        }*/

        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                //生成实体对应的key
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
                //被点赞的用户实体对应的key
                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);

                //判断该用户是否已点过赞
                boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);
                //启动事务
                operations.multi();

                if (isMember) {
                    //该用户已点过赞，再次点击取消赞（从set中移除）
                    operations.opsForSet().remove(entityLikeKey, userId);
                    //被点赞用户的赞数-1
                    operations.opsForValue().decrement(userLikeKey);
                }else {
                    //该用户未点赞，添加到set
                    operations.opsForSet().add(entityLikeKey, userId);
                    //被点赞用户的赞数+1
                    operations.opsForValue().increment(userLikeKey);
                }
                return operations.exec();
            }
        });
    }

    //查询某实体（帖子或评论）点赞的数量
    public long findEntityLikeCount(int entityType, int entityId) {
        //生成对应的key
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return redisTemplate.opsForSet().size(entityLikeKey);
    }

    //查询某人对某实体的点赞状态
    public  int findEntityLikeStatus(int userId, int entityType, int entityId) {
        //生成对应的key
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        //查看用户在此实体有没有点赞，如果为true(点赞)返回1.
        return redisTemplate.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
    }

    //查询某个用户获得点赞的数量
    public int findUserLikeCount(int userId) {
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        return count == null ? 0 : count.intValue();
    }

}
